import { Options } from '../entity/Options';
import { getZipName } from './parameter';
import { resolve } from 'node:path';
import fsm from 'fs/promises';
import { log } from './log';
import fs from 'node:fs';
import path from 'path';

export async function moveToOutputFolder(options: Options) {
  let zipName = getZipName(options);
  let sourcePath = resolve(`${options.cacheDir}/${zipName}`);
  let targetPath = resolve(options.outDir);
  let parentPath = path.dirname(targetPath);
  if (!fs.existsSync(parentPath)) {
    log.debug(`输出目录的父级不存在: '${parentPath}'， 即将创建`);
    fs.mkdirSync(parentPath, {recursive: true});
    log.debug(`创建成功`);
  }
  log.debug(`即将将应用从缓存目录: '${sourcePath}' 移动到输出目录: '${targetPath}'`);
  await fsm.rename(sourcePath, targetPath);
  log.debug(`移动成功`);
}
